import java.awt.Container;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Properties;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.ListModel;


public class Database implements DatabaseInterface{
	private ArrayList<Rollercoaster> CoasterArray ;
	private ArrayList<String> userNames;
	private Rollercoaster currentCoaster;
	private String  coasterName, currentUser;
	private boolean coasterTableEmpty, userTableEmpt;
	private static Connection con;
	private static  java.sql.PreparedStatement prep;
	private JList list;
	private ListModel model1;
	private int nameIndex, coasterIndex;
	private float coasterLenght, coasterX, coasterZ, coasterY;


	public Database() throws SQLException{
		//                              coasterLenght = currentCoaster.getTotalLength();
		//                              coasterLenght =  20;
		//                              coasterX = currentCoaster.getStartX();
		//                              coasterX = currentCoaster.getStartX();
		//                              coasterX = currentCoaster.getStartX();
		nameIndex = 0 ;
		coasterIndex = 0;
		coasterTableEmpty = true;
		userTableEmpt =  true;
		CoasterArray  =  new ArrayList<Rollercoaster>();
		userNames = new ArrayList<String>();
		//saveRollerCoaster(String rollerCoasterName, Rollercoaster rCoaster);
		//currentCoaster  = getCurrentCoaster();
		//coasterName = getcoasterName();

		//model1 = gui.userList.getModel();
		 //model1 = gui.model1;
		System.out.println(model1);

	}

	@Override
	public boolean coasterTableEmpty() {

		return coasterTableEmpty;
	}


	@Override
	public boolean userTableEmpty() {
		return userTableEmpt;
	}



	@Override
	public ArrayList<String> getAllCoasterNames() {

		return userNames;
	}


	@Override
	public String getUser() {
		return currentUser;
	}


	@Override
	public Rollercoaster getCurrentCoaster() {
		System.out.println(coasterIndex);
		return CoasterArray.get(coasterIndex);
	}


	@Override
	public String getcoasterName() {

		return coasterName;
	}


	@Override
	public void saveRollerCoaster(String rCoasterName, Rollercoaster rCoaster) {
		coasterName = rCoasterName;
		CoasterArray.add(coasterIndex, rCoaster);
		coasterIndex++;
	}


	@Override
	public ArrayList<Rollercoaster> getAllRollercoasters() {
		return null;
	}


	public void addUser(String name) {
		userNames.add(name);
		currentUser = name;
		nameIndex++;

	}


	//      @Override
	//      public ArrayList<Rollercoaster> getAllRollercoasters() {
	//              return null;//CoasterArray;
	//      }


	//      @Override
	//      public ArrayList <String> getAllUsers() {
	//              return null;//userNames;
	//      }


	public void createUserTable(Connection con) throws SQLException{
		//saveRollerCoaster(coasterName, currentCoaster);
		//String createString = "create table " + "UserTable " +
		//"User String, " +"RollerCoasterName String";
		Statement stmt = null;
		try {

			stmt = con.createStatement();
			stmt.executeUpdate("drop table if exists UserTable;");
			stmt.executeUpdate("create table UserTable (user, coastername);");
		} catch (SQLException e) {
			//some
		} finally {
			if (stmt != null)
			{ stmt.close();
			}
		}
	}
	public void createCoasterTable() throws SQLException{
		// andcurrentPart(Cart activecart)--->>> gonna have to do some parsin

		Statement stmt = null;
		try {

			stmt = con.createStatement();
			stmt.executeUpdate("drop table if exists coasterTable;");
			stmt.executeUpdate("create table UserTable ( coastername, coasterlenght, Xpart, Ypart, Zpart);");
		} catch (SQLException e) {
			//some
		} finally {
			if (stmt != null)
			{ stmt.close();
			}
		}
	}


	public void createCoasterpartsTable() throws SQLException{
		// andcurrentPart(Cart activecart)--->>> gonna have to do some parsing
		String createString  = "create table" +"CoasterTable"+
		"RollerCoasterName String, " +"coasterLenght float,"+
		"Xpart float," + "Ypart float,"+ "Zpart float,"+"parts String";



	}

	public void populateUserTable(Connection con) throws SQLException {
		userTableEmpt = false;
		Statement stmt = null;
		try {
			stmt = con.createStatement();
			prep = con.prepareStatement(
			"insert into UserTable values (?, ?);");
			for(int i = 0; i< model1.getSize(); i++){
				System.out.println(model1.getElementAt(i));
				prep.setString(1, (String) model1.getElementAt(i));
				prep.setString(2, "not yet created");
				prep.addBatch();
			}


			// con.setAutoCommit(false);
			prep.executeBatch();
			con.setAutoCommit(true);

		} catch (SQLException e) {
			//some
		} finally {
			if (stmt != null) { stmt.close(); }
		}
	}
	//      populate the coastertable with elements-->need to do some more though
	public void populateCoasterTable() throws SQLException {
		//                      stmt.executeUpdate("insert into " + ". Coaster" +
		//                      "values(currentCoaster, coasterLenght,coasterX, coasterY, coasterZ)");
		userTableEmpt = false;
		Statement stmt = null;

		try {
			stmt = con.createStatement();
			prep = con.prepareStatement(
			"insert into coasterTable values (?, ?);");
			//the actual populating act
			for(int i = 0; i< userNames.size(); i++){
				prep.setString(1, userNames.get(i));
				prep.setString(2, "not yet created");
				prep.addBatch();
			}


			// con.setAutoCommit(false);
			prep.executeBatch();
			con.setAutoCommit(true);

		} catch (SQLException e) {
			//some
		} finally {
			if (stmt != null) { stmt.close(); }
		}
	}
	//to retreive data from the usertable
	public void viewUserTable(Connection con) throws SQLException {
		Statement stmt = null;

		try {
			stmt = con.createStatement();
			prep.executeBatch();
			con.setAutoCommit(true);
			//ResultSet rs = stmt.executeQuery("select * UserTable;");
			ResultSet rs = stmt.executeQuery("select * from UserTable;");
			System.out.println("user");
			while (rs.next()) {
				System.out.println("user= " + rs.getString("user"));
				System.out.println("coastername = " + rs.getString("coastername"));
			}  

		} catch (SQLException e ) {
			//
		} finally {
			if (stmt != null) { stmt.close(); }
		}
	}
	public static void updateUserTable(String name, Connection con) throws SQLException{
		Statement stmt = null;
		System.out.println("evercalled");
		try {
			stmt = con.createStatement();
			prep = con.prepareStatement(
			"insert into UserTable values (?, ?);");
				prep.setString(1, name);
				prep.setString(2, "not yet created");
				prep.addBatch();
			// con.setAutoCommit(false);
			prep.executeBatch();
			con.setAutoCommit(true);

		} catch (SQLException e) {
			//some
		} finally {
			if (stmt != null) { stmt.close(); }
		}
	}
		
	public  void viewCoasterTable(Connection con) throws SQLException {
		Statement stmt = null;

		try {
			stmt = con.createStatement();
			prep.executeBatch();
			con.setAutoCommit(true);
			//ResultSet rs = stmt.executeQuery("select * UserTable;");
			ResultSet rs = stmt.executeQuery("select * from UserTable;");
			System.out.println("user");
			while (rs.next()) {
				System.out.println("user");
				System.out.println("coastername = " + rs.getString("coastername"));
				System.out.println("lenght = " + rs.getString("coasterlenght"));
				System.out.println("Xpart = " + rs.getString("Xpart"));
				System.out.println("Ypart = " + rs.getString("Ypart"));
				System.out.println("Zpart = " + rs.getString("Zpart"));
			}  

		} catch (SQLException e ) {
			//
		} finally {
			if (stmt != null) { stmt.close(); }
		}
	}


	public static void main(String [] args) throws SQLException, ClassNotFoundException{
		
		StartGUI gui =  new StartGUI();
		gui.setSize(1000,1000);
		gui.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		gui.setVisible(true);
		Class.forName("org.sqlite.JDBC");
		Connection con = DriverManager.getConnection("jdbc:sqlite:nowdb");
		Database  db =  new Database();
		db.createUserTable(con);
		db.populateUserTable(con);
		db.viewUserTable(con);

	}



}
